'use strict';

const Controller = require('egg').Controller;

const mysql = require('mysql')

const jwt = require('jsonwebtoken')

class HomeController extends Controller {

  //渲染
  async getList() {
    const { ctx, app } = this;
    const { value } = ctx.request.body
    const data = await app.mysql.get('lists')
    console.log(data);

    // const data = await app.mysql.query(`select * from lists where title like '%${value}%'`)

    ctx.body = {
      code: 200,
      data
    }
  }


  //登录
  async Login() {
    const { ctx, app } = this
    const { username, password } = ctx.request.body
    console.log(ctx.request.body);
    const data = await app.mysql.get('login', { username, password })
    const token = jwt.sign(username, 'xhs')
    if (data) {
      ctx.body = {
        code: 0,
        msg: '登录成功',
        token
      }
    } else {
      ctx.body = {
        code: 1,
        msg: '登录失败'
      }
    }
  }

  //注册
  async register() {
    const { ctx, app } = this

    const { username, password } = ctx.request.body

    const data = await app.mysql.get('login', { username })

    if (data) {
      ctx.body = {
        code: 3,
        msg: '账号已存在'
      }
    } else {
      await app.mysql.insert('login', { username, password })
      ctx.body = {
        code: 4,
        msg: '注册成功'
      }
    }
  }

  //删除
  async Del() {
    const { ctx, app } = this
    const { id } = ctx.request.body
    await app.mysql.delete('lists', { id })

    ctx.body = {
      code: 200,
      msg: '删除成功'
    }
  }

  //添加


  async screening1() {
    const { app, ctx } = this;
    const { value } = ctx.request.body;
    const data = await app.mysql.select("lists", { where: { city: value } })

    if (value === '区域') {
      ctx.body = {
        code: 1,
        data
      }
    } else {
      ctx.body = {
        code: 500,
        data
      }
    }
  }

  async screening2() {
    const { app, ctx } = this;
    const { value } = ctx.request.body;
    const data = await app.mysql.select("lists", { where: { type: value } })

    if (value === '户型') {
      ctx.body = {
        code: 1,
        data
      }
    } else {
      ctx.body = {
        code: 500,
        data
      }
    }
  }

  async screening3() {
    const { app, ctx } = this;
    const { value } = ctx.request.body;
    const data = await app.mysql.select("lists", { where: { desc: value } })

    if (value === '筛选') {
      ctx.body = {
        code: 1,
        data
      }
    } else {
      ctx.body = {
        code: 500,
        data
      }
    }
  }

  async screening5() {
    const { app, ctx } = this;
    const { value } = ctx.request.body;
    const data = await app.mysql.select("lists", { where: { zb: value } })

    if (value === '请选择') {
      ctx.body = {
        code: 1,
        data
      }
    } else {
      ctx.body = {
        code: 500,
        data
      }
    }
  }

  async screening6() {
    const { app, ctx } = this;
    const { value } = ctx.request.body;
    const data = await app.mysql.select("lists", { where: { staus: value } })

    if (value === '请选择') {
      ctx.body = {
        code: 1,
        data
      }
    } else {
      ctx.body = {
        code: 500,
        data
      }
    }
  }


  async search() {
    const { ctx, app } = this
    const { val } = ctx.request.body
    const data = await app.mysql.query(`select * from lists where title like '%${val}%'`)
    console.log(data);

    ctx.body = {
      code: 200,
      data
    }
  }

  async search1() {
    const { ctx, app } = this
    const { val } = ctx.request.body
    const data = await app.mysql.query(`select * from lists where price like '%${val}%'`)
    ctx.body = {
      code: 200,
      data
    }
  }

  async search2() {
    const { ctx, app } = this
    const { val } = ctx.request.body
    const data = await app.mysql.query(`select * from lists where name like '%${val}%'`)
    ctx.body = {
      code: 200,
      data
    }
  }

}

module.exports = HomeController;
